package com.wt.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wt.admin.entity.WqunArticleTag;
import com.wt.admin.entity.WqunArticleTagType;
import com.wt.admin.service.WqunArticleTagService;
import com.wt.admin.service.WqunArticleTagTypeService;
import com.wt.commonutils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 标签表 前端控制器
 * </p>
 *
 * @author wt
 * @since 2023-10-13
 */
@Api(tags = "文章标签")
@CrossOrigin
@RestController
@RequestMapping("/wqunArticleTag")
public class WqunArticleTagController {

    @Autowired
    private WqunArticleTagService wqunArticleTagService;

    @Autowired
    private WqunArticleTagTypeService wqunArticleTagTypeService;

    @ApiOperation("添加文章标签")
    @PostMapping("/addArticleTag")
    @PreAuthorize("hasAnyAuthority('admin:wqunArticleTag:addArticleTag')")
    public R addArticleTag(@RequestBody WqunArticleTag wqunArticleTag){
        QueryWrapper<WqunArticleTag> wqunArticleTagQueryWrapper = new QueryWrapper<>();
        wqunArticleTagQueryWrapper.eq("name",wqunArticleTag.getName());
        int count = wqunArticleTagService.count(wqunArticleTagQueryWrapper);
        if(count > 0){
            return R.error().message("该标签已经存在");
        }

        return wqunArticleTagService.save(wqunArticleTag)?R.ok().message("添加成功"):R.error().message("添加失败");
    }

    @ApiOperation("修改文章标签")
    @PostMapping("/updateArticleTag")
    @PreAuthorize("hasAnyAuthority('admin:wqunArticleTag:updateArticleTag')")
    public R updateArticleTag(@RequestBody WqunArticleTag wqunArticleTag){
        boolean save = wqunArticleTagService.updateById(wqunArticleTag);
        if(save){
            return R.ok().message("修改成功");
        }else{
            return R.error().message("修改失败");
        }
    }

    @ApiOperation("删除文章标签")
    @PostMapping("/deleteArticleTag")
    @PreAuthorize("hasAnyAuthority('admin:wqunArticleTag:deleteArticleTag')")
    public R deleteArticleTag(@RequestBody WqunArticleTag wqunArticleTag){
        WqunArticleTag byId = wqunArticleTagService.getById(wqunArticleTag.getId());

        byId.setIsDelete("1");
        boolean b = wqunArticleTagService.updateById(byId);
        if(b){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("删除失败");
        }
    }

    @ApiOperation("分页查询文章标签")
    @GetMapping("/findAllArticleTag/{pageSize}/{currentPage}/{selectFind}/{selectFindValue}")
    @PreAuthorize("hasAnyAuthority('wqunArticleTag:findAllArticleById')")
    public R findAllArticleTag(@PathVariable int pageSize,@PathVariable int currentPage,@PathVariable String selectFind,@PathVariable String selectFindValue){
        return wqunArticleTagService.findAllArticleTag(pageSize,currentPage,selectFind,selectFindValue);
    }

    @ApiOperation("查找所有文章标签")
    @GetMapping("/findAllTag")
    @PreAuthorize("hasAnyAuthority('wqunArticleTag:findAllTag')")
    public R findAllTag(){
        QueryWrapper<WqunArticleTagType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete","0");
        List<WqunArticleTagType> findAllTag = wqunArticleTagTypeService.list(queryWrapper);
        if(findAllTag != null && findAllTag.size() != 0){
            return R.ok().data("findAllTag",findAllTag);
        }else{
            return R.error().message("获取数据异常");
        }
    }

    @ApiOperation("根据id查找所有文章标签")
    @GetMapping("/findAllArticleTagById/{id}")
    @PreAuthorize("hasAnyAuthority('wqunArticleTag:findAllArticleById')")
    public R findAllArticleTagById(@PathVariable("id") String id){
        WqunArticleTag findAllArticleTagById = wqunArticleTagService.getById(id);
        if(findAllArticleTagById != null){
            return R.ok().data("findAllArticleTagById",findAllArticleTagById);
        }else{
            return R.error().message("查询异常");
        }
    }
}

